/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * Includes generic functions for affecting the physics world
 */

#include "GameWorldModule.h"
#include "../objects/physicobjects/elements/GameConstructionElement.h"
#include "../objects/physicobjects/GamePhysicObject.h"
#include "../objects/physicobjects/bombs/GameBomb.h"

// generic force constant, may be varied to change overall explosions of the game
//float FORCE_MULTIPLIER = 150.0f;
//// treshold whether to apply explosion or not
//float FORCE_TRESHOLD = 3.0f;

// explode all the bombs
void GameWorldModule::explodeAvailableBombs( std::vector< GamePhysicObject* > *objects ) {
	// loop through objects and explode bombs
	for( unsigned int i = 0; i < objects->size(); ++i ) {
		if( objects->at( i )->getObjectType() > ELEMENT_INVALID && objects->at( i )->getObjectType() < BOMB_INVALID ) {
			dynamic_cast< GameBomb* >( objects->at( i ) )->detonate();
		}
	}
}

// cause a single physics world explosion with a specific force (0.0 = none, 1.0 = infinite)
void GameWorldModule::causePhysicWorldExplosion( b2World *world, std::vector< GamePhysicObject* > *objects, b2Vec2 pos, float force ) {
	// loop through objects and apply impulses
	for( unsigned int i = 0; i < objects->size(); ++i ) {
		if( objects->at( i )->getObjectType() < ELEMENT_INVALID && objects->at( i )->getObjectType() != ELEMENT_GROUND ) {
			// let the object decide what to do
			dynamic_cast< GameConstructionElement* >( objects->at( i ) )->applyExplosionToElements( pos, force );

			//std::vector< b2Body* > *b = objects->at( i )->getBodyObjects();
			//// loop through all the body objects
			//for( unsigned int j = 0; j < b->size(); ++j ) {
			//	b2Vec2 explosionImpulse;
			//	explosionImpulse.Set(
			//		b->at( j )->GetPosition().x - pos.x,
			//		b->at( j )->GetPosition().y - pos.y );
			//	explosionImpulse.Normalize();
			//	explosionImpulse *= ( FORCE_MULTIPLIER * force );
			//	// apply explosion if convenient
			//	if( std::abs( explosionImpulse.x ) > FORCE_TRESHOLD && std::abs( explosionImpulse.y ) > FORCE_TRESHOLD )
			//		b->at( j )->ApplyLinearImpulse( explosionImpulse, b->at( j )->GetPosition() );

			//	// TODO: check whether we should break the element
			//}
		}
	}
}
